
//通过aida64获取电脑信息 老版本
const char *host = "192.168.0.107";  //自己PC的ip地址


void PC_infor() //通过AIDA64，获取PC硬件使用信息
{               //参考https://blog.csdn.net/weixin_42487906/article/details/119990801
                // http://www.taichi-maker.com/homepage/iot-development/iot-dev-reference/esp8266-c-plus-plus-reference/wificlient/connect/

  if (!client.connect(host, 80))
  {
    Serial.println("Connect host failed!");
    return;
  }
  Serial.println("host Conected!");

  String getUrl = "/sse";
  client.print(String("GET ") + getUrl + " HTTP/1.1\r\n" + "Content-Type=application/json;charset=utf-8\r\n" + "Host: " + host + "\r\n" + "User-Agent=ESP32\r\n" + "Connection: close\r\n\r\n");
  Serial.println("Get send");

  delay(10);

  char endOfHeaders[] = "\n\n";
  bool ok = client.find(endOfHeaders);
  if (!ok)
  {
    Serial.println("No response or invalid response!");
  }
  Serial.println("Skip headers");

  String line = "";

  line += client.readStringUntil('\n');

  Serial.println("Content:");
  Serial.println(line);

  int16_t dataStart = 0;
  int16_t dataEnd = 0;
  String dataStr;

  char gpu_use[] = "GPU Utilization";
  dataStart = line.indexOf(gpu_use) + strlen(gpu_use);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  gpu_usage = dataStr.toInt();

  char cpuUsage[] = "CPU Utilization";
  dataStart = line.indexOf(cpuUsage) + strlen(cpuUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  cpu_usage = dataStr.toInt();

  /*char ramUsage[] = "Memory Utilization";
  dataStart = line.indexOf(ramUsage) + strlen(ramUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  ram_usage = dataStr.toInt();

  Serial.print("GPU usage :");
  Serial.println(gpu_usage);
  Serial.print("CPU usage :");
  Serial.println(cpu_usage);
  Serial.print("RAM usage :");
  Serial.println(ram_usage);*/

  spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLACK);

  spr.fillSprite(TFT_BLACK);
  spr.setTextColor(TFT_YELLOW, TFT_BLACK);
  spr.setTextFont(2);
  spr.setTextSize(2);
  spr.setCursor(20, 8);
  spr.print("CPU: ");
  spr.print(cpu_usage);
  spr.println("%");
  spr.setCursor(20, 40);
  spr.print("GPU: ");
  spr.print(gpu_usage);
  spr.println("%");
  // spr.print("RAM: "); spr.print(ram_usage);spr.println("%");//不显示内存占用率
  spr.pushSprite(0, 0);
}

void PC_infor1() //通过AIDA64，获取PC硬件使用信息
{                //参考https://blog.csdn.net/weixin_42487906/article/details/119990801
                 // http://www.taichi-maker.com/homepage/iot-development/iot-dev-reference/esp8266-c-plus-plus-reference/wificlient/connect/
  if (!client.connect(host, 80))
  {
    Serial.println("Connect host failed!");
    return;
  }
  Serial.println("host Conected!");

  String getUrl = "/sse";
  client.print(String("GET ") + getUrl + " HTTP/1.1\r\n" + "Content-Type=application/json;charset=utf-8\r\n" + "Host: " + host + "\r\n" + "User-Agent=ESP32\r\n" + "Connection: close\r\n\r\n");
  Serial.println("Get send");

  delay(10);

  char endOfHeaders[] = "\n\n";
  bool ok = client.find(endOfHeaders);
  if (!ok)
  {
    Serial.println("No response or invalid response!");
  }
  Serial.println("Skip headers");

  String line = "";

  line += client.readStringUntil('\n');

  Serial.println("Content:");
  Serial.println(line);

  int16_t dataStart = 0;
  int16_t dataEnd = 0;
  String dataStr;

  char gpu_use[] = "GPU Utilization";
  dataStart = line.indexOf(gpu_use) + strlen(gpu_use);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  gpu_usage = dataStr.toInt();

  /*char cpuUsage[] = "CPU Utilization";
  dataStart = line.indexOf(cpuUsage) + strlen(cpuUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  cpu_usage = dataStr.toInt();

  char ramUsage[] = "Memory Utilization";
  dataStart = line.indexOf(ramUsage) + strlen(ramUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  ram_usage = dataStr.toInt();

  Serial.print("GPU usage :");
  Serial.println(gpu_usage);
  Serial.print("CPU usage :");
  Serial.println(cpu_usage);
  Serial.print("RAM usage :");
  Serial.println(ram_usage);*/

  spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLACK);

  spr.fillSprite(TFT_BLACK);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.setTextFont(2);
  spr.setTextSize(2);
  // spr.setCursor(20, 8);
  // spr.print("CPU: "); spr.print(cpu_usage); spr.println("%");
  // spr.setCursor(20, 40);
  // spr.pushImage(0,0,64,64,bmp2_lbili);
  spr.setCursor(8, 24);
  spr.print("GPU: ");
  spr.print(gpu_usage);
  spr.println("%");
  // spr.print("RAM: "); spr.print(ram_usage);spr.println("%");//不显示内存占用率
  spr.pushSprite(0, 0);
}

void PC_infor2() //通过AIDA64，获取PC硬件使用信息
{                //参考https://blog.csdn.net/weixin_42487906/article/details/119990801
                 // http://www.taichi-maker.com/homepage/iot-development/iot-dev-reference/esp8266-c-plus-plus-reference/wificlient/connect/
  if (!client.connect(host, 80))
  {
    Serial.println("Connect host failed!");
    return;
  }
  Serial.println("host Conected!");

  String getUrl = "/sse";
  client.print(String("GET ") + getUrl + " HTTP/1.1\r\n" + "Content-Type=application/json;charset=utf-8\r\n" + "Host: " + host + "\r\n" + "User-Agent=ESP32\r\n" + "Connection: close\r\n\r\n");
  Serial.println("Get send");

  delay(10);

  char endOfHeaders[] = "\n\n";
  bool ok = client.find(endOfHeaders);
  if (!ok)
  {
    Serial.println("No response or invalid response!");
  }
  Serial.println("Skip headers");

  String line = "";

  line += client.readStringUntil('\n');

  Serial.println("Content:");
  Serial.println(line);

  int16_t dataStart = 0;
  int16_t dataEnd = 0;
  String dataStr;

  /*char gpu_use[] = "GPU Utilization";
  dataStart = line.indexOf(gpu_use) + strlen(gpu_use);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  gpu_usage = dataStr.toInt();*/

  char cpuUsage[] = "CPU Utilization";
  dataStart = line.indexOf(cpuUsage) + strlen(cpuUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  cpu_usage = dataStr.toInt();

  /*char ramUsage[] = "Memory Utilization";
  dataStart = line.indexOf(ramUsage) + strlen(ramUsage);
  dataEnd = line.indexOf("%", dataStart);
  dataStr = line.substring(dataStart, dataEnd);
  ram_usage = dataStr.toInt();

  Serial.print("GPU usage :");
  Serial.println(gpu_usage);
  Serial.print("CPU usage :");
  Serial.println(cpu_usage);
  Serial.print("RAM usage :");
  Serial.println(ram_usage);*/

  spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLACK);

  spr.fillSprite(TFT_BLACK);
  spr.setTextColor(TFT_RED, TFT_BLACK);
  spr.setTextFont(2);
  spr.setTextSize(2);

  spr.setCursor(8, 24);
  spr.print("CPU: ");
  spr.print(cpu_usage);
  spr.println("%");
  // spr.print("RAM: "); spr.print(ram_usage);spr.println("%");//不显示内存占用率
  spr.pushSprite(0, 0);
}
